<%@ page import="org.apache.struts.action.Action"%><%@ page import="com.mbbmap.util.Constants"%><script src="resource/jquery-1.10.2.js" type="text/javascript"></script>
<script type="text/javascript">
	var NN4 = (document.layers) ? true : false
	var IE4 = (document.layers) ? false : true
	// The timeout remainder should be set 30 seconds
	// before the real time out (10 minutes).
	var timeoutLimit = 13.5; // 9.5 minutes
	//var timeoutLimit = 1.5; // 4.5 minutes - TIMEOUT
	//var timeoutLimit = 1.0; // 4.5 minutes - TIMEOUT
	timeoutLimit *= 60 * 1000; // convert to miliseconds
	var lastEvent;
	function display_dialog_box() {		//alert('1');
		var date = new Date();
		var date2;
		var hours = convertTimeDisplay(date.getHours());
		var minutes = convertTimeDisplay(date.getMinutes());
		var seconds = convertTimeDisplay(date.getSeconds());
		if(confirm("WARNING:\nYour session will expire in 30 seconds starting from "+hours+":"+minutes+":"+seconds+".\nPress OK to continue, or Cancel to logout.")) {	
			date2 = new Date();
			var timeAfterResponse = date2.getTime();
			lastEvent = document.timer.lastEvent.value;
			//alert("timeAfterResponse1="+timeAfterResponse);
			//alert("lastEvent1="+lastEvent);
			var a = timeoutLimit+30000;
			//alert("timeoutLimit+30000=1=>" + a);
			var b = parseInt(timeAfterResponse) - parseInt(lastEvent);
			//alert("parseInt(timeAfterResponse) - parseInt(lastEvent)=>" + b);
			if((parseInt(timeAfterResponse) - parseInt(lastEvent)) >= (timeoutLimit+30000)) {
				//alert("Your session has expired 01");
				go_to_logout();
			}
			setTimeout('check_for_timeout()', timeoutLimit);
			//document.timer.submit();
		} else {
			date2 = new Date();
			var timeAfterResponse = date2.getTime();
			lastEvent = document.timer.lastEvent.value;
			if((parseInt(timeAfterResponse) - parseInt(lastEvent)) >= (timeoutLimit+30000)) {
				//alert("Your session has expired 02");
				go_to_logout();			}			go_to_logout();		}	}
	function check_for_timeout() {
		var date = new Date();
		var currentTime = date.getTime();
		lastEvent = document.timer.lastEvent.value;
		if((parseInt(currentTime) - parseInt(lastEvent)) < timeoutLimit) {
			setTimeout('check_for_timeout()', 10000);
		} else {
			//alert(parseInt(currentTime)+" - "+parseInt(lastEvent)+" = "+(parseInt(currentTime) - parseInt(lastEvent))+" < "+ timeoutLimit);
			display_dialog_box();
		}
	}
	function convertTimeDisplay(num) {
		if (num < 10)
			return new String("0"+num);
		else
			return new String(num);
	}
	function updateLastEvent() {				//alert('update last event');
		var date = new Date();
		var time = date.getTime();
		if(document.timer != null){
			if(document.timer.lastEvent != null){
				//alert("updateLastEvent=>"+document.timer.lastEvent.value);
				document.timer.lastEvent.value = time;	
				//alert("updateLastEvent2=>"+document.timer.lastEvent.value);				}		}
		setTimeout('check_for_timeout()', 10000);
	}
</script>
<script type="text/javascript">	function go_to_logout() {
		this.location='<%=response.encodeURL("LogOut.do?dispatch=init")%>';
	}
</script>
<form name="timer" action="" target="_self" method="post" style="display:none;">
	<input type="hidden" name="lastEvent" value="" />
</form><script type="text/javascript">$(document).ready(function(){	updateLastEvent();	$('body').click(function() {		   //alert('body click');		   updateLastEvent();	});	});</script>